//
//  YJDRZView.m
//  YJDSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDRZView.h"
@interface YJDRZView()<YJDTextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation YJDRZView

- (YJDButton *)titleIMG
{
    if (!_titleIMG)
    {
        _titleIMG = [[YJDButton alloc] init];
        _titleIMG.height = TITLE_HEIGHT;
        _titleIMG.style = YJDButtonTitleStyleCameo;
        _titleIMG.userInteractionEnabled = NO;
        [self addSubview:_titleIMG];
        [_titleIMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.size.mas_offset(CGSizeMake(70, TITLE_HEIGHT));
            make.centerY.equalTo(self.backBtn);
        }];
    }
    return _titleIMG;
}

- (YJDTextField *)nameTF
{
    if (!_nameTF)
    {
        _nameTF = [[YJDTextField alloc] init];
        _nameTF.type = YJDTextFieldTypeAccountNormal;
        _nameTF.keyboardType = UIKeyboardTypeDefault;
        _nameTF.returnKeyType = UIReturnKeyNext;
        _nameTF.customDelegate = self;
        [self addSubview:_nameTF];
        [_nameTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(GAP * 2.0 + BACKBUTTON_HEIGHT);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _nameTF;
}


- (YJDTextField *)IDTF
{
    if (!_IDTF)
    {
        _IDTF = [[YJDTextField alloc] init];
        _IDTF.type = YJDTextFieldTypeIDCart;
        _IDTF.customDelegate = self;
        [self addSubview:_IDTF];
        [_IDTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.nameTF.mas_bottom).with.offset(GAP / 2.0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _IDTF;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self.delegate;
        _tableView.dataSource = self.delegate;
        [self addSubview:_tableView];
        [_tableView mas_updateConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.IDTF.mas_bottom).with.offset(-3);
            make.right.equalTo(self.IDTF);
            make.left.equalTo(self.IDTF).with.offset(42);
            make.height.mas_equalTo(0);
        }];
    }
    return _tableView;
}

- (YJDTextField *)numTF
{
    if (!_numTF)
    {
        _numTF = [[YJDTextField alloc] init];
        _numTF.type = YJDTextFieldTypeDefault;
        _numTF.keyboardType = UIKeyboardTypeAlphabet;
        [self addSubview:_numTF];
        [_numTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.IDTF.mas_bottom).with.offset(GAP / 2.0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _numTF;
}

- (YJDButton *)authenticateBtn
{
    if (!_authenticateBtn)
    {
        _authenticateBtn = [[YJDButton alloc] init];
        _authenticateBtn.height = TEXTFILDHEIGHT;
        [_authenticateBtn addTarget:self action:@selector(yjd_actionForAuthenticateButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_authenticateBtn];
        [_authenticateBtn mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.numTF.mas_bottom).with.offset(GAP / 2.0);
            make.size.mas_offset(CGSizeMake((self.bounds.size.width - GAP * 2.0), BUTTONHEIGHT));
        }];
    }
    return _authenticateBtn;
}

- (void)yjd_actionForBackButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDRZViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDRZViewWithEvent:YJDRZViewEventBack dict:nil];
    }
}

- (void)yjd_actionForAuthenticateButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDRZViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDRZViewWithEvent:YJDRZViewEventAuthenticate dict:@{@"bindcardid":self.numTF.text,@"bindcardtype":self.IDTF.text,@"bindname":self.numTF.text}];
    }
}

#pragma mark - YJDTextFieldDelegate
- (void)yjd_actionForXiaLa:(BOOL)isXiaLa
{
    self.tableView.hidden = !isXiaLa;
    if (self.tableView.hidden == NO)
    {
        [self.tableView reloadData];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType == UIReturnKeyNext)
    {
        [self.numTF becomeFirstResponder];
        return NO;
    }
    return YES;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
